//
//  MyController.m
//  20240823
//
//  Created by 张伟 on 2024/8/31.
//

#import "MyController.h"
#import "MefunctionModel.h"
#import "MeFunctionCell.h"

@interface MyController ()

@property (weak, nonatomic) IBOutlet UILabel *accountLabel;
@property (weak, nonatomic) IBOutlet UILabel *mealLabel;
@property (nonatomic) BOOL enableProtection;


@end

@implementation MyController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)setupUI {
    self.title = @"个人中心";
    if (UserModel.shared.account != nil) {
        _accountLabel.text = [@"当前账号：" stringByAppendingString:UserModel.shared.account];
    }
    _mealLabel.text = @"当前未购买套餐";
    [self.tableView cellWithNib:MeFunctionCell.class];
    [self configData];
}

-(void)configData {
    [self.dataSource addObject:[MeFunctionModel initWithId:1 icon:@"购买套餐" name:@"购买套餐"]];
    [self.dataSource addObject:[MeFunctionModel initWithId:2 icon:@"充值记录" name:@"充值记录"]];
    [self.dataSource addObject:[MeFunctionModel initWithId:3 icon:@"联系我们" name:@"联系我们"]];

    if (self.enableProtection) {
        [self.dataSource addObject:[MeFunctionModel initWithId:4 icon:@"停用" name:@"停用密码验证"]];
    }else{
        [self.dataSource addObject:[MeFunctionModel initWithId:4 icon:@"启用" name:@"启用密码验证"]];
    }
    
    [self.dataSource addObject:[MeFunctionModel initWithId:5 icon:@"注销账号" name:@"注销账号"]];
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeFunctionCell * cell = [tableView dequeueWithClass:MeFunctionCell.class];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MeFunctionModel * model = self.dataSource[indexPath.row];
    
    switch (model.id) {
        case 1: {
            //购买套餐页面
            NSLog(@"%@",localizedStringKey(@"home"));
        }
            break;
        case 2: {
            //购买记录
            
        }
            break;
        case 3: {
            //联系我们
            
        }
            break;
        case 4: {
            //开启/关闭生物验证
            if (self.enableProtection) {
                UIAlertController * controller = [UIAlertController alertControllerWithTitle:@"是否确定取消使用安全保护" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [controller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                    [NSUserDefaults.standardUserDefaults setValue:@(false) forKey:PROTECTION];
                    [NSUserDefaults.standardUserDefaults synchronize];
                    self.dataSource[3] = [MeFunctionModel initWithId:4 icon:@"启用" name:@"启用密码验证"];
                    [self.tableView reloadData];
                }]];
                [self presentViewController:controller animated:true completion:nil];
            }else{
                MJWeakSelf
                [ZWTools authenticateUserWithCompletion:^(BOOL success, NSError * _Nullable error) {
                    if (success) {
                        [NSUserDefaults.standardUserDefaults setValue:@(true) forKey:PROTECTION];
                        [NSUserDefaults.standardUserDefaults synchronize];
                        weakSelf.dataSource[3] = [MeFunctionModel initWithId:4 icon:@"停用" name:@"停用密码验证"];
                        [weakSelf.tableView reloadData];
                    }
                }];
            }
            
        }
            break;
        case 5: {
            //注销账号
            UIAlertController * controller = [UIAlertController alertControllerWithTitle:@"请确认是否注销账号" message:@"账号一旦被注销，将无法正常使用" preferredStyle:UIAlertControllerStyleAlert];
            
            [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.placeholder = @"请输入登录密码";
                textField.secureTextEntry = true;
            }];
            
            [controller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            
            [controller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                NSString * pwd = controller.textFields[0].text;
                NSDictionary * dic = @{
                    @"id":@(UserModel.shared.userId),
                    @"password":pwd
                };
                [NetService requestDeactivateAccount:dic successHandle:^{
                    [ZWTools cleanUserData];
                    [VCRouter toLoginController];
                } fail:^{
                    
                }];
            }]];
            [self presentViewController:controller animated:true completion:nil];
        }
            break;
        default:
            break;
    }
    
}

- (BOOL)enableProtection {
    return [[NSUserDefaults.standardUserDefaults valueForKey:PROTECTION] boolValue];
}

@end
